[contents] [next] [bottom] (1 out of 17)

Chapter 2 - ScriptX Building Blocks

This chapter describes many of the basic building blocks of the ScriptX language, including expression syntax, comments, lexical constructs such as end-of-line, literals such as numbers and strings, variable scope, declaration, assignment, and simple operators.

Expressions

ScriptX is an expression-based language. An expression is a language construct that can be evaluated to yield a value. In ScriptX, every complete construct in the language yields a value, and that value is always an object.

Valid expressions in ScriptX include things that you might not consider to be expressions in other languages, such as loops and conditionals. Since every construct in ScriptX yields a value, you can write more flexible code by nesting expressions within other expressions and passing around their results.

When a ScriptX expression is evaluated, the resulting value is always an object, and that object is an instance of a class (either one of the ScriptX core classes or a class you have defined). Where the distinction between the value and the class of an object is important, this book refers to each separately. For example, the expression 2 + 2 yields an ImmediateInteger object whose value is 4.

End-of-Line

ScriptX is a flexible, command-based language which, unlike some other programming languages, does not require statement punctuators such as semicolons. In ScriptX, an end-of-line most commonly signifies the end of an expression. You can break an expression over multiple lines, but only if the break occurs at a point where the expression is incomplete.

For example, you could write the expression a < b as follows:

a <
b

In this example, the end of the expression has not yet occurred at the end of the first line, so ScriptX looks to the next line for the expression to be completed. That same expression could not be written this way:

a
< b

Because a is a complete expression in ScriptX, the end-of-line after a is considered the end of the expression. The second line would then result in an error, since the ScriptX bytecode compiler expects to find a new expression beginning on that line.

A sentence is a complete syntactic construct that can be evaluated by the ScriptX bytecode interpreter. The bytecode interpreter does not evaluate an expression until you enter a "complete sentence" in the Listener window. An incomplete sentence can be continued on the following line. The following generalizations apply to completion of ScriptX sentences.

  1. When the interpreter is expecting a closing delimiter, such as a parenthesis, square bracket, or brace, the sentence is incomplete.
  2. A complete sentence cannot end with a separator, such as a comma, colon, or function body separator ( -> ).
  3. A complete sentence cannot end with a binary operator, such as an arithmetic or comparison operator ( +, -, *, /, =, ==, <>, !=, !==, <, >, <=, >=), the collection access operator ([ ]), the function operator (( )), or the assignment operator (:=).
Line breaks can make a ScriptX program less readable. Some programmers use the backslash ( \ ) to indicate all line breaks, even in cases where it is not required by ScriptX syntax. Most programmers break lines only in obvious places, such as after separators and binary operators.

A script cannot be broken in the middle of a literal (such as a number or string) without causing a syntax error, or changing the meaning of the expression.

-- this example generates a syntax error
global myArray := #(33, 44, 55, 6
6, 77, 88, 99)
-- ** Syntax error: ("syntax error") at "6" (SyntaxError)

In the next example, the line break is interpreted as a newline character:

global myString := "This is my
string"
"This is my
string"

To force evaluation of an incomplete sentence in the scripter, type !! and press enter. Two exclamation marks act as a terminator, allowing you to begin entering a new expression. If the expression that the scripter was currently evaluating is incomplete, termination reports a syntax error.

(a + b) < !!
-- ** Syntax error: ("syntax error") at "(a + b) < !" (SyntaxError)

For more information on ScriptX syntax and expressions, see Appendix A, "ScriptX Reference."

Continuation Over Multiple Lines

Most ScriptX expressions can be written on a single line. Some expressions have recognizable boundaries that allow them to be broken over multiple lines. If an expression is too long to fit on a single line, and cannot be divided, you can continue an expression on the next line by placing a backslash ( \ ) at the end of the line.

A backslash indicates that an expression continues on the next line. The backslash itself is treated as white space. If you prefer, you can use a backslash wherever there is a line break, for readability. When an example in this book uses the backslash, the second line is indented to show continuation from the previous line.

1 + 2 + 3 \
	+ 4 + 5

Since tabs are treated as blank space in ScriptX, you can use tabs freely to make your ScriptX code more readable. One place you might not want to use a tab is in a string-you wouldn't want tab characters embedded in your strings.

print "This is an example of a very long expression, one that \ 
is too long to fit on one line." 

The example above shows a call to the print function. Function calls are the most common case of an expression that is too long to fit on one line but cannot be split over multiple lines unless you use a backslash. Function calls are described in further detail in Chapter 3, "Working with Objects."

Finally, multiple expressions can be placed on one line, separated by semicolons:

t := b * b; if t < j then t else j; print b; print t


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.